package com.tydic.mapboxtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.tydic.mapboxtest.Model.MassifResp;
import com.tydic.mapboxtest.Model.MassifShapeResp;
import com.tydic.mapboxtest.Model.PlotModel;
import com.tydic.mapboxtest.util.FileUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestActivity extends AppCompatActivity {
    MassifShapeResp massifShapeResp;
    MassifResp massifResp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }

    public void parseJson(View view) {
        try {
            long startTime = System.nanoTime();
            Gson gson = new Gson();
            massifShapeResp = gson.fromJson(FileUtil.readBigFile(this, "massif_shape_small.txt"), MassifShapeResp.class);
            long endTime = System.nanoTime();
            long val = (endTime - startTime) / 1000000;
            Log.d("TestActivity", "解析MassifShapeResp cost " + val);

            long startTime1 = System.nanoTime();
            massifResp = gson.fromJson(FileUtil.readBigFile(this, "massif.txt"), MassifResp.class);
            long endTime1 = System.nanoTime();
            long val1 = (endTime1 - startTime1) / 1000000;
            Log.d("TestActivity", "解析MassifResp cost " + val1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void dealJson(View view) {
        try {
            long startTime = System.nanoTime();

            dealJsonData1();

            long endTime = System.nanoTime();
            long val = (endTime - startTime) / 1000000;
            Log.d("TestActivity", "dealJson cost " + val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理地块json信息
     */
    private List<PlotModel> dealJsonData() {
        List<PlotModel> polygonList = new ArrayList<>();

        try {
            for (MassifShapeResp.ContractRootBean.ResultsBean.MassifCollactionBean collactionBean : massifShapeResp.getContractRoot().getResults().getMassifCollaction()) {

                PlotModel plotModel = new PlotModel();
                List<LatLng> polygons = new ArrayList<>();

                for (MassifResp.ContractRootBean.ResultsBean massif : massifResp.getContractRoot().getResults()) {
                    if (massif.getMassifId().equals(collactionBean.getMassifId())) {
                        List<List<Double>> placeJson = collactionBean.getPlaceJson();
                        for (List<Double> doubles : placeJson) {
                            if (doubles.size() < 2) {
                                continue;
                            }
                            LatLng latLng = new LatLng();
                            latLng.setLongitude(doubles.get(0));
                            latLng.setLatitude(doubles.get(1));
                            polygons.add(latLng);
                        }

                        plotModel.setLatLng(polygons);
                        plotModel.setClick(false);
                        plotModel.setCODE(massif.getMassifCode());
                        plotModel.setId(massif.getMassifId());
                        plotModel.setName(massif.getMassifName());
                        plotModel.setCenterLatlng(new LatLng(Double.valueOf(massifShapeResp.getContractRoot().getResults().getCENTER_GIS_X()),
                                Double.valueOf(massifShapeResp.getContractRoot().getResults().getCENTER_GIS_Y())));

                        polygonList.add(plotModel);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return polygonList;
    }

    /**
     * 处理地块json信息
     */
    private List<PlotModel> dealJsonData1() {
        List<PlotModel> polygonList = new ArrayList<>();

        try {
            MassifShapeResp.ContractRootBean.ResultsBean results = massifShapeResp.getContractRoot().getResults();

            Map<String, MassifResp.ContractRootBean.ResultsBean> massifMap = massifList2Map(massifResp.getContractRoot().getResults());

            for (MassifShapeResp.ContractRootBean.ResultsBean.MassifCollactionBean collactionBean : results.getMassifCollaction()) {

                PlotModel plotModel = new PlotModel();
                List<LatLng> polygons = new ArrayList<>();

                List<List<Double>> placeJson = collactionBean.getPlaceJson();
                for (List<Double> doubles : placeJson) {
                    if (doubles.size() < 2) {
                        continue;
                    }
                    LatLng latLng = new LatLng();
                    latLng.setLongitude(doubles.get(0));
                    latLng.setLatitude(doubles.get(1));
                    polygons.add(latLng);
                }
                plotModel.setLatLng(polygons);
                plotModel.setCenterLatlng(new LatLng(Double.valueOf(results.getCENTER_GIS_X()),
                        Double.valueOf(results.getCENTER_GIS_Y())));

                plotModel.setClick(false);
                MassifResp.ContractRootBean.ResultsBean massif = massifMap.get(collactionBean.getMassifId() + "");
                if (massif != null) {
                    plotModel.setCODE(massif.getMassifCode());
                    plotModel.setId(massif.getMassifId());
                    plotModel.setName(massif.getMassifName());
                }

                polygonList.add(plotModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return polygonList;
    }

    public Map<String, MassifResp.ContractRootBean.ResultsBean> massifList2Map(List<MassifResp.ContractRootBean.ResultsBean> massifs) {
        long startTime = System.nanoTime();

        Map<String, MassifResp.ContractRootBean.ResultsBean> map = new HashMap<>();

        for (MassifResp.ContractRootBean.ResultsBean massif : massifs) {
            map.put(massif.getMassifId(), massif);
        }

        long endTime = System.nanoTime();
        long val = (endTime - startTime) / 1000000;
        Log.d("TestActivity", "massifList2Map cost " + val);
        return map;
    }
}
